package com.itheima.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.itheima.bean.ArticleContext;
import com.itheima.bean.ArticleList;
import com.itheima.bean.FrontArticleVO;
import com.itheima.dto.PageArticles;
import com.itheima.dto.YddArticleDTO;
import com.itheima.entity.*;
import com.itheima.mapper.YddAdvimgMapper;
import com.itheima.mapper.YddArticleMapper;
import com.itheima.mapper.YddLinkMapper;
import com.itheima.result.BaseContext;
import com.itheima.result.PageData;
import com.itheima.service.SysConfigService;
import com.itheima.service.YddArticleService;
import com.itheima.service.YddCateService;
import com.itheima.service.YddTagService;
import com.itheima.vo.*;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.time.Duration;
import java.time.Instant;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

/**
 * @projectName: kafka
 * @package: com.itheima.service.impl
 * @className: YddArticleServiceImpl
 * @author: Eric
 * @description: TODO
 * @date: 2023/9/23 21:22
 * @version: 1.0
 */
@Service
@RequiredArgsConstructor
public class YddArticleServiceImpl implements YddArticleService {
    private final YddArticleMapper yddArticleMapper;
    private final YddCateService yddCateService;
    private final SysConfigService sysConfigService;
    private final YddTagService yddTagService;
    private final YddLinkMapper yddLinkMapper;
    private final YddAdvimgMapper yddAdvimgMapper;

    @Override
    public PageData pag(PageArticles pageArticles) {
        PageHelper.startPage(pageArticles.getPage(), pageArticles.getLimit());
        List<YddArticle> yddArticleList = yddArticleMapper.pag(pageArticles.getTitle(),
                pageArticles.getCateid(),
                pageArticles.getStart_time(),
                pageArticles.getEnd_time());
        PageInfo<YddArticle> pageInfo = new PageInfo<>(yddArticleList);

        List<YddArticleVO> yddArticleVOList = new ArrayList<>();
        for (YddArticle yddArticle : yddArticleList) {
            YddArticleVO yddArticleVO = new YddArticleVO();
            BeanUtils.copyProperties(yddArticle, yddArticleVO);
            yddArticleVO.setCreate_date(yddArticle.getCreateDate());
            yddArticleVO.setUpdate_date(yddArticle.getUpdateDate());
            yddArticleVO.setUser_id(yddArticle.getUserId());
            yddArticleVO.setDefault_data(yddArticle.getDefaultData());
            String catename = yddArticleMapper.selectCateName(yddArticle.getCateid());
            yddArticleVO.setCatename(catename);
            yddArticleVOList.add(yddArticleVO);
        }
        return new PageData(pageInfo.getTotal(),
                pageInfo.getPages(),
                pageInfo.getPageSize(),
                pageInfo.getPageNum(),
                yddArticleVOList);
    }

    @Override
    public YddArticleVO queryArticle(Integer id) {
        YddArticle yddArticle = yddArticleMapper.queryArticle(id);
        YddArticleVO yddArticleVO = new YddArticleVO();
        BeanUtils.copyProperties(yddArticle, yddArticleVO);
        yddArticleVO.setCreate_date(yddArticle.getCreateDate());
        yddArticleVO.setUpdate_date(yddArticle.getUpdateDate());
        return yddArticleVO;
    }

    @Override
    public void deleteArticle(Integer id) {
        yddArticleMapper.deleteArticle(id);
    }

    @Override
    public void updateArticle(YddArticleDTO yddArticleDTO) {
        Integer count = yddArticleMapper.selectCountByTitleNotId(yddArticleDTO.getTitle(), yddArticleDTO.getId());
        if (count > 0) {
            throw new RuntimeException("标题已存在!");
        }
        YddArticle yddArticle = new YddArticle();
        BeanUtils.copyProperties(yddArticleDTO, yddArticle);
        yddArticle.setUpdateDate(yddArticleDTO.getUpdate_date());
        yddArticleMapper.updateArticle(yddArticle);
    }

    @Override
    public void creatArticle(YddArticleDTO yddArticleDTO) {
        Integer count = yddArticleMapper.selectCountByTitle(yddArticleDTO.getTitle());
        if (count > 0) {
            throw new RuntimeException("标题已存在!");
        }
        YddArticle yddArticle = new YddArticle();
        BeanUtils.copyProperties(yddArticleDTO, yddArticle);
        yddArticle.setCreateDate(yddArticleDTO.getCreate_date());
        yddArticle.setUserId(Integer.parseInt(BaseContext.getLoginUserId()));
        yddArticleMapper.creatArticle(yddArticle);
    }

    @Override
    public FrontArticleVO topArticles(Integer page, Integer limit) {
        FrontArticleVO frontArticleVO = new FrontArticleVO();
        // 获取当前时间戳  结束时间戳为当前时间戳
        Instant now = Instant.now();
        // 计算起始时间戳，向前推24小时
        Instant dayTime = now.minus(Duration.ofHours(24));
        Instant weekTime = now.minus(Duration.ofDays(7));
        Long endTime = now.toEpochMilli();
        Long dayStartTime = dayTime.toEpochMilli();
        Long weekStartTime = weekTime.toEpochMilli();
        Integer dayStartCount = yddArticleMapper.selectCountByTime(dayStartTime, endTime);
        Integer weekStartCount = yddArticleMapper.selectCountByTime(weekStartTime, endTime);
        System.out.println("------------dayStartCount---------------" + dayStartCount);
        System.out.println("------------weekStartCount---------------" + weekStartCount);

        PageArticles pageArticles = new PageArticles();
        pageArticles.setStart_time(null);
        pageArticles.setEnd_time(null);
        pageArticles.setTitle(null);
        pageArticles.setCateid(null);
        pageArticles.setPage(page);
        pageArticles.setLimit(limit);
        PageData pageData = pag(pageArticles);

        frontArticleVO.setDesc("接口描述");
        frontArticleVO.setNum_day(dayStartCount);
        frontArticleVO.setNum_week(weekStartCount);
        frontArticleVO.setList(pageData);
        return frontArticleVO;
    }

    @Override
    public ArticleContext selectHotArticles() {
        ArticleContext articleContext = new ArticleContext();
        List<ArticleHot> articleHotList = yddArticleMapper.selectHotArticles();
        articleContext.setDesc("侧边最新文章");
        articleContext.setList(articleHotList);
        return articleContext;
    }

    @Override
    public ArticleContext selectRecommendArticles() {
        ArticleContext articleContext = new ArticleContext();
        List<ArticleHot> articleHotList = yddArticleMapper.selectRecommendArticles();
        articleContext.setDesc("侧边推荐文章");
        articleContext.setList(articleHotList);
        return articleContext;
    }

    @Override
    public YddArticleShowVO showArticles(Integer id) {
        System.out.println("--------------id-----------------"+id);
        YddArticleShowVO yddArticleShowVO = new YddArticleShowVO();

        List<YddArticleShowPart> allArticleShowPart = yddArticleMapper.allArticleShowPart();
        System.out.println("------------------allArticleShowPart-------------------------"+allArticleShowPart);
        YddArticleShowPart yddArticleShowPart1 = new YddArticleShowPart();
        YddArticleShowPart yddArticleShowPart2 = new YddArticleShowPart();
        for (YddArticleShowPart yddArticleShowPart : allArticleShowPart) {
            if (yddArticleShowPart.getId() < id) {
                yddArticleShowPart1 = yddArticleShowPart;
            }
            if (yddArticleShowPart.getId()>id){
                System.out.println("--------------yddArticleShowPart.getId()----------------"+yddArticleShowPart.getId());
                yddArticleShowPart2 = yddArticleShowPart;
                break;
            }
        }
        if (Objects.isNull(yddArticleShowPart1)){   // 若为第一个
            yddArticleShowPart1 = allArticleShowPart.get(allArticleShowPart.size()-1);
        }
        if (Objects.isNull(yddArticleShowPart2)){   // 若为最后一个
            yddArticleShowPart2 = allArticleShowPart.get(0);
        }
        List<YddArticleShowPart> yddArticleShowParts1 = new ArrayList<>();
        yddArticleShowParts1.add(yddArticleShowPart1);
        List<YddArticleShowPart> yddArticleShowParts2 = new ArrayList<>();
        yddArticleShowParts2.add(yddArticleShowPart2);
        System.out.println("--------------yddArticleShowParts1-----------------"+yddArticleShowParts1);
        System.out.println("--------------yddArticleShowParts2-----------------"+yddArticleShowParts2);


        YddArticle yddArticle = yddArticleMapper.queryArticle(id);
        System.out.println("------------yddArticle----------------"+yddArticle);
        YddArticleVO yddArticleVO = new YddArticleVO();
        BeanUtils.copyProperties(yddArticle, yddArticleVO);
        yddArticleVO.setCreate_date(yddArticle.getCreateDate());
        yddArticleVO.setUpdate_date(yddArticle.getUpdateDate());
        yddArticleVO.setUser_id(yddArticle.getUserId());
        yddArticleVO.setDefault_data(yddArticle.getDefaultData());
        String cateName = yddArticleMapper.selectCateName(yddArticle.getCateid());
        yddArticleVO.setCatename(cateName);
        System.out.println("------------yddArticleVO----------------"+yddArticleVO);


        List<YddArticleRecommend> recommend = yddArticleMapper.selectByCateId(yddArticleVO.getCateid());
        System.out.println("--------------recommend----------------"+recommend);

        yddArticleShowVO.setPrev(yddArticleShowParts1);
        yddArticleShowVO.setNext(yddArticleShowParts2);
        yddArticleShowVO.setInfo(yddArticleVO);
        yddArticleShowVO.setRecommend(recommend);

        return yddArticleShowVO;
    }

    @Override
    public ArticleList articleList(Integer page, Integer limit, Integer cateid) {
        ArticleList articleList = new ArticleList();

        PageHelper.startPage(page,limit);
        List<YddArticle> yddArticleList = yddArticleMapper.queryAllArticle();
        PageInfo<YddArticle> pageInfo = new PageInfo<>(yddArticleList);
        List<YddArticleVO> yddArticleVOList = new ArrayList<>();
        for (YddArticle yddArticle : yddArticleList) {
            YddArticleVO yddArticleVO = new YddArticleVO();
            BeanUtils.copyProperties(yddArticle,yddArticleVO);
            yddArticleVO.setCreate_date(yddArticle.getCreateDate());
            yddArticleVO.setUpdate_date(yddArticle.getUpdateDate());
            yddArticleVO.setUser_id(yddArticle.getUserId());
            yddArticleVO.setDefault_data(yddArticle.getDefaultData());
            yddArticleVOList.add(yddArticleVO);
        }

        PageData pageData = new PageData();
        pageData.setCount(pageInfo.getTotal());
        pageData.setTotalPages(pageInfo.getPages());
        pageData.setPageSize(pageInfo.getPageSize());
        pageData.setCurrentPage(pageInfo.getPageNum());
        pageData.setData(yddArticleVOList);

        articleList.setList(pageData);
        return articleList;
    }

    @Override
    public IndexAllDataVO indexData() {
        List<YddCateVO> yddCateVOList = yddCateService.queryAllCate();
        System.out.println("================yddCateVOList=================="+yddCateVOList);
        ConfigNew configNew = sysConfigService.getAllConfigurations();
        System.out.println("================configNew=================="+configNew);
        List<YddTagVO> yddTagVOList = yddTagService.queryAllTag();
        System.out.println("================yddTagVOList==================="+yddTagVOList);
        List<YddLinkVO> yddLinkVOList = new ArrayList<>();
        List<YddLink> yddLinkList = yddLinkMapper.queryAllLink();
        for (YddLink yddLink : yddLinkList) {
            YddLinkVO yddLinkVO = new YddLinkVO();
            BeanUtils.copyProperties(yddLink,yddLinkVO);
            yddLinkVO.setDefault_data(yddLink.getDefaultData());
            yddLinkVOList.add(yddLinkVO);
        }
        System.out.println("=================yddLinkVOList===================="+yddLinkVOList);
        String advimgpos1 = "15";
        String advimgpos2 = "14";
        String advimgpos3 = "14";
        String advimgpos4 = "12";
        List<YddAdvimg> banner = yddAdvimgMapper.selectAdvimgList(advimgpos1);
        List<YddAdvimg> sideBanner = yddAdvimgMapper.selectAdvimgList(advimgpos2);
        List<YddAdvimg> showBanner1 = yddAdvimgMapper.selectAdvimgList(advimgpos3);
        List<YddAdvimg> showBanner2 = yddAdvimgMapper.selectAdvimgList(advimgpos4);
        showBanner1.addAll(showBanner2);
        System.out.println("=================banner================"+banner);
        System.out.println("=================sideBanner================"+sideBanner);
        System.out.println("=================showBanner1================"+showBanner1);

        IndexAllDataVO indexAllDataVO = new IndexAllDataVO();
        indexAllDataVO.setAllCate(yddCateVOList);
        indexAllDataVO.setConfig(configNew);
        indexAllDataVO.setAllTag(yddTagVOList);
        indexAllDataVO.setAllLink(yddLinkVOList);
        indexAllDataVO.setBanner(banner);
        indexAllDataVO.setSideBanner(sideBanner);
        indexAllDataVO.setShowBanner(showBanner1);
        return indexAllDataVO;
    }
}
